﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharp.NetFramework.Entities
{
  /// <summary>
  /// 问题范围类型
  /// refs:https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/enum?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DZH-CN%26k%3Dk(enum_CSharpKeyword)%3Bk(TargetFrameworkMoniker-.NETFramework%2CVersion%3Dv4.6.1)%3Bk(DevLang-csharp)%26rd%3Dtrue
  /// </summary>
  public enum QuestionScopeType
  {
    FLOOR = 1,
    ROOM = 2
  }

  public static class QuestionScopeTypeExtension
  {
    public static int ToInt32(this QuestionScopeType type)
    {
      return (int)type;
    }
  }

  public class QualityQuestionDetail : Entity
  {

    /// <summary>
    /// 
    /// </summary>
    public Guid QualityQuestionDetailGUID { get; set; }
    /// <summary>
    /// 质量问题GUID
    /// </summary>
    public Guid? QualityQuestionGUID { get; set; }
    /// <summary>
    /// 项目GUID
    /// </summary>
    public Guid? ProjectGUID { get; set; }
    /// <summary>
    /// 楼栋GUID
    /// </summary>
    public Guid? BuildingGUID { get; set; }
    /// <summary>
    /// 问题范围GUID
    /// </summary>
    public Guid? QuestionScopeGUID { get; set; }
    /// <summary>
    /// 问题范围名称(楼层、房间名称)
    /// </summary>
    public string QuestionScopeName { get; set; }
    /// <summary>
    /// 问题范围类别(1,楼层、2,房间)
    /// </summary>
    public int? QuestionScopeType { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public Guid? ParentScopeGUID { get; set; }

    /// <summary>
    /// 导航属性
    /// </summary>
    //public virtual QualityQuestion Question { get; set; }

  }

  /// <summary>
  /// 
  /// </summary>
  public abstract class Entity
  {

    /// <summary>
    /// 实体状态 用于EF CUD操作使用 (多用于处理集合类导航属性SaveChanges)
    /// </summary>
    public EntityState EntityState { get; set; } = EntityState.Detached;

  }

  public static class EntityExtension
  {
    public static IEnumerable<T> Added<T>(this ICollection<T> collection) where T : Entity
    {
      return collection.Where(m => m.EntityState == EntityState.Added).AsEnumerable();
    }

    public static IEnumerable<T> Modified<T>(this ICollection<T> collection) where T : Entity
    {
      return collection.Where(m => m.EntityState == EntityState.Modified).AsEnumerable();
    }

    public static IEnumerable<T> Deleted<T>(this ICollection<T> collection) where T : Entity
    {
      return collection.Where(m => m.EntityState == EntityState.Deleted).AsEnumerable();
    }

  }

  public enum EntityState
  {
    /// <summary>
    /// 已分离
    /// </summary>
    Detached,

    /// <summary>
    /// 已添加
    /// </summary>
    Added,

    /// <summary>
    /// 修改
    /// </summary>
    Modified,

    /// <summary>
    /// 已删除
    /// </summary>
    Deleted
  }
}

